﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Tank
{
    public class Frame
    {
        Rectangle rect;
        public int x;
        public int y;
        public int width;
        public int height;
    }

    public class Animation
    {
        public enum PlayDirection
        {
            FORWARD = 1,
            REVERSE,
            STOP,
            PAUSE
        }

        public enum PlaySpeed
        {
            NORMAL = 1,
            FASTFWD,
            FASTFWD2X,
            FASTFWD4X,
            REWIND,
            REWIND2X,
            REWIND4X
        }
                
        // Members
        //========================================================
        string textFileName;
        float elapsedTime;
        float speed;
        uint currFrameIndx;
        uint numFrames;
        Frame[] frames;
        PlayDirection direction;

        public Animation(string textureFile, float playSpeed, uint totlFrames, int frameWidth, int frameHeight,
                         int startX, int startY, PlayDirection playDir)
        {
            textFileName = textureFile;
            speed = playSpeed;
            numFrames = totlFrames;
            currFrameIndx = 0;
            elapsedTime = 0;
            direction = playDir;

            InitFrames(startX, startY, frameWidth, frameHeight);

        }


        private void InitFrames(int startX, int startY, int width, int height)
        {
            frames = new Frame[numFrames];
            int currX = startX, currY = startY;
            for(int i = 0; i < numFrames; ++i)
            {
                frames[i] = new Frame();
                frames[i].x = currX;
                frames[i].y = currY;
                frames[i].width = width;
                frames[i].height = height;
                currX += width;
            }
        }


        public void SetFrame(int index, int startX, int startY, int width, int height)
        {
            frames[index].x = startX;
            frames[index].y = startY;
            frames[index].width = width;
            frames[index].height = height;
        }

        public Frame Update(float deltaTime)
        {
            // Check if it is time to update to next frame
            elapsedTime += deltaTime;
            if (elapsedTime < speed)
                return frames[currFrameIndx];

            elapsedTime = 0;

            switch (direction)
            {
                case PlayDirection.FORWARD:
                {
                    ++currFrameIndx;
                    if (currFrameIndx > frames.Length - 1)
                    {
                        currFrameIndx = 0;
                    }
                    return frames[currFrameIndx];
                }

                case PlayDirection.REVERSE:
                {
                    --currFrameIndx;
                    if (currFrameIndx > frames.Length - 1)
                    {
                        currFrameIndx = (uint)frames.Length - 1;
                    }
                    return frames[currFrameIndx];
                }

                case PlayDirection.STOP:
                    return frames[0];

                case PlayDirection.PAUSE:
                    return frames[currFrameIndx];

                default:
                    return frames[0];
            }
        }

        public PlayDirection Direction
        {
            set { direction = value; }
        }

        public string TextFile
        {
            get { return textFileName; }
        }

    }
}
